No. Remember that Java is single inheritance.
Programming in Java consists mostly of creating class hierarchies and instantiating objects from them. The Java Development Kit gives you a rich collection of base classes that you can extend to do your work.
Following is a program that uses a class Video
to represent videos available
at a store.
Inheritance is not explicitly used in this program (so far).
Note to AP students:
Study the toString()
method and how it is used in main()
to print a description of each object.
This is a common technique.
Several questions on AP-CS tests in the past few years have
involved writing a toString()
method for a class.
You can copy this program to an editor, save it, and run it.
class Video { String title; // name of the item int length; // number of minutes boolean avail; // is the video in the store? // constructor public Video( String ttl ) { title = ttl; length = 90; avail = true; } // constructor public Video( String ttl, int lngth ) { title = ttl; length = lngth; avail = true; } public String toString() { return title + ", " + length + " min. available:" + avail ; } } public class VideoStore { public static void main ( String args[] ) { Video item1 = new Video("Jaws", 120 ); Video item2 = new Video("Star Wars" ); System.out.println( item1.toString() ); System.out.println( item2.toString() ); } }
What will this program print?